﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace TradeForm.config
{


    public partial class FrmPosSettings : Form
    {
        private string cfgFileName = string.Empty;

        public FrmPosSettings()
        {
            InitializeComponent();
        }

        private void FrmPosSettings_Load(object sender, EventArgs e)
        {


        }

        private void BindData(string filename)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataSet1.Clear();
            this.dataSet1.ReadXml(AppDomain.CurrentDomain.BaseDirectory + filename);
            DataTableCollection tables = this.dataSet1.Tables;
            DataView view1 = new DataView(tables[0]);
            bindingSource1.DataSource = view1;
            dataGridView1.DataSource = bindingSource1;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.cfgFileName) && File.Exists(AppDomain.CurrentDomain.BaseDirectory + this.cfgFileName))
            {
                XElement printSettings = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + this.cfgFileName);
                IEnumerable<XElement> settings = null;
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        settings = (from el in printSettings.Elements("setting")
                                    where (string)el.Attribute("name") == row.Cells["ColName"].Value.ToString()
                                    select el);
                        if (settings != null)
                        {
                            settings.First().SetAttributeValue("PosX", row.Cells["ColPosX"].Value.ToString());
                            settings.First().SetAttributeValue("PosY", row.Cells["ColPosY"].Value.ToString());
                        }
                        else
                        {

                        }
                    }
                }
                try
                {
                    printSettings.Save(AppDomain.CurrentDomain.BaseDirectory + this.cfgFileName);
                    MessageBox.Show("保持成功。");
                }
                catch
                {
                    MessageBox.Show("保持失败。");
                }

            }
            else
            {
                MessageBox.Show("文件不存在。");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //Settings sets = new Settings();
            //Setting set = new Setting();
            //set.Name = "aaa";
            //set.CName = "明年国产";
            //set.PosX = 1";
            //set.PosY = "3";
            //sets.settings.Add(set);
            //XmlSerializer x = new XmlSerializer(typeof(Settings));
            //TextWriter writer = new StreamWriter("aa.xml");
            //x.Serialize(writer, sets);
            this.Close();
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridView gdv = (DataGridView)sender;
            gdv.Rows[e.RowIndex].ErrorText = "";
            if (e.FormattedValue != null && !string.IsNullOrEmpty(e.FormattedValue.ToString()) &&
                (gdv.Columns[e.ColumnIndex].Name == "ColPosX" || gdv.Columns[e.ColumnIndex].Name == "ColPosY"))
            {
                if (gdv.Rows[e.RowIndex].IsNewRow) { return; }
                try
                {
                    decimal cellValue = Convert.ToDecimal(e.FormattedValue);
                }
                catch
                {
                    e.Cancel = true;
                    gdv.Rows[e.RowIndex].ErrorText = "只能输入数字";

                }
            }
        }

        private void rdCustomSet_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdCustomSet.Checked)
            {
                this.cfgFileName = @"\config\CustomBillSettings.xml";
                BindData(this.cfgFileName);
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton1.Checked)
            {
                this.cfgFileName = @"\config\ContractSettings.xml";
                BindData(this.cfgFileName);
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton3.Checked)
            {
                this.cfgFileName = @"\config\ProformaInvoiceSettings.xml";
                BindData(this.cfgFileName);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton2.Checked)
            {
                this.cfgFileName = @"\config\InvoiceSettings.xml";
                BindData(this.cfgFileName);
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton4.Checked)
            {
                this.cfgFileName = @"\config\ShipmentSettings.xml";
                BindData(this.cfgFileName);
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton5.Checked)
            {
                this.cfgFileName = @"\config\PackListSettings.xml";
                BindData(this.cfgFileName);
            }
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton6.Checked)
            {
                this.cfgFileName = @"\config\PostalOrder.xml";
                BindData(this.cfgFileName);
            }
        }
    }
}
